;Draws the tilemap.



#macro DrawHL(y, x)

 
 push de
 push bc
 push af
 push ix
 push hl
 LD hl,x*256+y
 LD (curRow),hl
 pop hl
 push hl
 B_CALL _DispHL
 B_CALL _getKey
 pop hl
 pop ix
 pop af
 pop bc
 pop de
 
#endmacro
	
Tile_width = 2
Map_Entry_Size = 1
MAP_MASK = $ff
PMAPWIDTH = 6
PMAPHEIGHT = 5
PMAPHIEGHT = PMAPHEIGHT
delta = savesscreen


Start:
	

	ld hl, Splash_Screen
	ld de, plotsscreen
	ld bc, 768
	ldir
	ld hl, plotsscreen
	call safeCopy
	B_CALL _getKey

	di
	;Timing in the game, including "forcing" FPS depending
	;on what speed we want, is done via interrupts.
	;This is to achieve a consistant speed.
	;So we need to load interrupt information.

	ld hl, Play_One_SAD_Frame
	ld de, $9A9A
	ld bc, End_SAD_Frame - Play_One_SAD_Frame
	ldir

	ld a, $99
	ld i, a



    LD     HL, $9900
    LD     DE, $9901
    LD     BC, 256
	ld A, $9a
    LD     ($9900), A
    LDIR

;This is where the game is loaded.  Thus this code is run only once.

	xor a			;Start with 0 buildings.
	ld (BuildingCount), a


;The game requires user RAM.

	ld hl,21000
	ld de,$9d95
	bcall(_InsertMem)

;Savescreen is used for saving data involving maps.  Thus, we need to disable
;the APD so that the data will not be corrupted.


	RES apdAble,(IY+apdFlags)

;The Map Data, containing the tiles in tilemap form.  Maps can be no bigger than
;3600 bytes, but a 3600 byte map requires 1800 bytes for fog-of-war and collision
;information.  Therefore, 5400 bytes is needed in this area.

MapData .equ $9D96 + 5000

MapTileConditions .equ MapData + 3600 + 2
;Tile Data.  Requires 8000 bytes.

TileData .equ MapData + 5402

;Structure_Locations holds the locations of all buildings that could possibly exit,
;up to 200.  Four bytes for each building: TileX, TileY, 6 bits for the building's
;ID, and 10 bits for the buildings HP.

Structure_Locations .equ TileData + 4100
Ship_Frame_Data .equ Structure_Locations + 2000


;Every building has unique data for it.  For instance, defensive buildings
;require X and Y for their weapon positions.  Refineries require
;the number of processing units they contain.
;Cloaking fields need to tell the calculator how far their cloak
;extends.

;Structure Tags stores the data that these buildings require.

Player1 .equ $9D95 + 18500
Structure_Tags .equ Player1 + 200


	

;To optimize the drawing of structures

Store_Structure_Sprite_Addresses:

;	B_CALL _Load_Sprite_Data
	B_CALL _Load_Map_Data
	B_CALL _Load_Tile_Data


;Saves the value of certain flags so that we can use
;them for our program.

	ld a, (IY + trigflags)
	ld (FlagPreserve1), a

	ld hl, 1000
	ld (Player1 + Player_Money), hl
	
	
	ld hl,2
	ld (delta),hl
	ld (DeltaPreserve), hl
	ld a,1
	ld (Num_Layers),a
	xor a
	ld h,a
	ld a,(mapdata)
	ld l,a
	ld (mapheight),hl
	ld a,(mapdata+1)
	ld l,a
	ld (Mapwidth),hl
	ld hl,0
	ld (MapY),hl
	ld (MapX),hl
	ld hl,TileData
	ld (Tileptr),hl
	ld hl,MapData+2
	ld (MAPptr),hl
	ld hl,Mapbuf1
	ld (MapbufPtr),hl
	ld hl,premap
	ld (PreMapPTR),hl

	call Updatemapfull

	ld iy, flags

	ld hl, plotsscreen
	call safecopy


	IM 2
	ld a, %00000110
	out (4), a
	
	ei
		

	ld a, Balkstone
	ld (Player1 + Player_Unit), a
	ld a, 7
	ld (Player1 + Player_Direction), a
	ld (Player1 + Player_Current_Direction), a

	ld hl, 0*256+60
	ld (Player1 + Player_Map_X), hl
	ld hl, 0*256+40
	ld (Player1 + Player_Map_Y), hl

	SET Load_Ship_Sprite, (IY + flag + 1)


Loop:

	call updatemap

	ld a,(MapY)
	and tile_width*8-1
	ld h,a
	ld e,PMAPWIDTH*Tile_width
	call HxE

	ld de,mapbuf1
	add hl,de


	
	ld de, plotsscreen
	ld bc, 768
	ldir
	ld iy, flags
_
	call Context_Sensitive_Get_Key
	call Draw_Ship
	ld iy, flags
	BIT Ship_Mode, (IY + flag + 2)
	jr z, _
	call Move_Ship_At_Speed
_
	
	BIT MenuMode, (IY + flag)
	jr z, _
	call Draw_Menu
	ld a, (MenuIdentity)
	cp GroupOfShipsMenu 
	jr nz, _

	call Draw_Ship_Group_Information

_
	BIT Cursor_Mode, (IY + flag)
	jr z, _
	call Draw_Select_Cursor

 	call Check_Cursor_Over_Building


		
_
	BIT Draw_Standard_Alert_Box, (IY + flag + 2)
	call nz, Alert_Player

	BIT Display_Belthium_Earned, (IY + flag + 2)
	call nz, Display_Resources_Earned


	ld hl, plotsscreen
	call safecopy

	
	jr Loop





Play_One_SAD_Frame:

	di

	EX AF, AF'
	EXX
	push ix
	push iy
	ld iy, flags

	call Continue_Building_Construction

	
	call Play_Ship_Animation

	ld	a,%00001000
	out	(3),a
	ld	a,%00001010
	out	(3),a

	BIT Draw_Standard_Alert_Box, (IY + flag + 2)
	jr z, _

	ld hl, (Standard_Alert_Timer)
	inc hl
	ld de, 300
	call cphlde
	ld (Standard_Alert_Timer), hl

	jr nz, _


	
	RES Draw_Standard_Alert_Box, (IY + flag + 2)

_

	Call Increase_Player_Money

	EX AF, AF'
	EXX
	pop iy
	pop ix

	ei
	RET


End_SAD_Frame:




Alert_Player:

;First, place the text on the screen.

	ld hl, InfoBox + 12
	ld de, plotsscreen
	ld bc, 12*7
	ldir

	ld hl, 1 * 256 + 2

	ld (penCol), hl

	ld hl, (Standard_Alert_Text)
	Call Put_Text_Zero_End

	 ld hl, plotsscreen
 	ld b, 12 * 7

 ;HL is the position at plotsscreen to start.
 ;B is the number of bytes in plotsscreen the box should span.

_

 	ld a, %11111111

 	xor (hl)

 	ld (hl), a

	 inc hl

 	djnz -_

 	ret  

	ret


Increase_Player_Money:

	
 ld a, (Gain_Additional_Money_Timer)
 inc a
 ld (Gain_Additional_Money_Timer), a

 ;Increase belthium amount by the number mined
 ;per second ONLY if one second has passed

  cp 120
  ret nz

 xor a

 ld (Gain_Additional_Money_Timer), a

 ld hl, (Player1 + Player_Money)
 ld de, (Player1 + Money_Per_Second)
 add hl, de

 ld (Player1 + Player_Money), hl

 ret







_
	ld (delta),hl

	bit 5,a
	jr nz,skipspeedon
	in a,(2)
	rla
	jr nc,skipspeedon
	ld a,1
	out ($20),a
	ld a,$FF
skipspeedon:
	bit 6,a
	jr nz,skipspeedoff
	in a,(2)
	rla
	jr nc,skipspeedoff
	xor a
	out ($20),a
	ld a,$FF
skipspeedoff:

	ld a,$FF
	out (1),a
	ld a,$FD
	out (1),a
	nop
	nop
	in a,(1)
	bit 6,a
	jp nz,loop
	;ld hl,$9D95
	;ld de, 12000
	;bcall(_DelMem)
	;bcall(_JForceCmdNoChar)


movemapdown:
	ld hl,(mapY)
	ld de,(delta)
	add hl,de
	push hl
		ld hl,(mapheight)
		ld de,PMAPHEIGHT-2
		or a
		sbc hl,de
		sla l
		rl h
		sla l
		rl h
		sla l
		rl h
		sla l
		rl h
	pop de
	call cphlde
	jr c,okaydown
	ex de,hl
okaydown:
	ld (mapY),hl
	ret


movemapup:
	ld hl,(mapY)
	ld de,(delta)
	or a
	sbc hl,de
	jr nc,notzeroup
	ld hl,0
notzeroup:
	ld (mapY),hl
	ret

	
movemapright:

	ld hl,(mapx)
	ld de,(delta)
	add hl,de
	push hl
		ld hl,(mapwidth)
		ld de,PMAPWIDTH
		or a
		sbc hl,de
		sla l
		rl h
		sla l
		rl h
		sla l
		rl h
		sla l
		rl h
	pop de
	ex de,hl
	call cphlde
	jr c,okayright
	ex de,hl
okayright:
	ld (mapx),hl
	ret


movemapleft:

	ld hl,(mapx)
	ld de,(delta)
	or a
	sbc hl,de
	jr nc,notzeroleft
	ld hl,0
notzeroleft:
	ld (mapx),hl
	ret
CpHLDE:
	or a
	sbc hl,de
	add hl,de
	ret
	
SafeCopy:	
	di
	ld c,$10
	ld a,$80
setrow:
	in f,(c)
	jp m,setrow
	out ($10),a
	ld de,12
	ld a,$20
col:
	in f,(c)
	jp m,col
	out ($10),a
	push af
	ld b,64
row:
	ld a,(hl)
rowwait:
	in f,(c)
	jp m,rowwait
	out ($11),a
	add hl,de
	djnz row
	pop af
	dec h
	dec h
	dec h
	inc hl
	inc a
	cp $2c
	jp nz,col
	ei
	ret

PMAPBUFSIZE = PMAPWIDTH*PMAPHEIGHT*8*Tile_width*Tile_width
PREMAPBUFSIZE = (PMAPWIDTH+1)*PMAPHEIGHT
PreMap = $9d95
Mapbuf1 = $9D95+2500
MapperRam = savesscreen+2

#include "Graymapper.inc"

Updatemapfull:
		
	call DrawMapFull
	ld a,(MapY)
	and tile_width*8-1
	ld h,a
	ld e,PMAPWIDTH*Tile_width
	call HxE

	ld de,mapbuf1
	add hl,de


	
	ld de, plotsscreen
	ld bc, 768
	ldir

	ret


Map_X = MapX
Map_Y = MapY



Splash_Screen:

	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11110000,%00000000,%01111111,%11111111,%11110000,%00000000,%00011111,%11111111,%11111100,%00000000,%00011111
	.db %10111111,%11100000,%00000000,%01111111,%11111111,%11100000,%00000000,%00011111,%11111111,%11111000,%00000000,%00001111
	.db %11111111,%11100000,%00000000,%01111011,%11111111,%11100000,%00000000,%00011111,%11111111,%11111000,%00000000,%00001111
	.db %11111111,%11000011,%11111111,%11111111,%11111111,%11000011,%11111100,%00111111,%11111111,%11110000,%11111111,%00001111
	.db %11111101,%11000011,%11111111,%11111111,%11101111,%11000011,%11111100,%00111111,%11110111,%11110000,%11111111,%00001111
	.db %11111111,%10000111,%11111111,%11111111,%11111111,%10000111,%11111000,%01111111,%11111111,%11100001,%11111110,%00011111
	.db %11111111,%00001111,%10111111,%11111111,%11111111,%00001111,%11110000,%11111101,%11111111,%11000011,%11111100,%00111011
	.db %11111111,%00001111,%11111111,%11111111,%11111111,%00001111,%11110000,%11111111,%11111111,%11000011,%11111100,%00111111
	.db %11111110,%00011111,%11111111,%11111111,%11111110,%00011111,%11100001,%11111111,%11111111,%10000111,%11111000,%01111111
	.db %01111110,%00000000,%00000111,%11011111,%11111110,%00000000,%00000001,%11111111,%11111111,%00001111,%11110000,%11111111
	.db %11111100,%00000000,%00000111,%11111111,%11111100,%00000000,%00000011,%11111111,%11011111,%00001111,%11110000,%11111111
	.db %11111100,%00000000,%00000111,%11111111,%10111100,%00000000,%00000011,%11101111,%11111110,%00011111,%11100001,%11111111
	.db %11111100,%00000000,%00001111,%11111111,%11111100,%00000000,%00000111,%11111111,%11111100,%00111111,%11000001,%11110111
	.db %11111111,%11111111,%00001111,%11111111,%11111000,%01111111,%10000111,%11111111,%11111100,%00111111,%11000011,%11111111
	.db %10111111,%11111110,%00011111,%11111111,%11110000,%11111111,%00001111,%11111111,%11111000,%01111111,%10000111,%11111111
	.db %11111111,%11111110,%00011111,%10111111,%01110000,%11111111,%00001111,%11111111,%11111000,%01111111,%10000111,%11111111
	.db %11111110,%11111100,%00111111,%11111111,%11100001,%11111110,%00011111,%11111111,%11110000,%11111111,%00001111,%11111111
	.db %11111111,%11111000,%01111111,%11111111,%11000011,%11011100,%00111111,%11111111,%11100001,%11111110,%00011111,%11111111
	.db %11111111,%11111000,%01111100,%00111111,%11000011,%11111100,%00111110,%00011111,%11100001,%11111110,%00011111,%11000011
	.db %11000000,%00000000,%11111000,%00111111,%10000111,%11111000,%01111100,%00011111,%11000000,%00000000,%00111111,%10000011
	.db %11000000,%00000000,%11111000,%00111111,%10000111,%11111000,%01111100,%00011111,%11000000,%00000000,%00111111,%10000011
	.db %11000000,%00000001,%11111000,%01111111,%10001111,%11111000,%11111100,%00111101,%11000000,%00000000,%01111111,%10000111
	.db %11111111,%11111111,%11111111,%11111011,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%10000001,%10000001,%10000001,%11111000,%11100111,%11000000,%11001110,%01110001,%11111100,%00111111,%11111111
	.db %11111111,%10011100,%10011100,%10011111,%11111000,%11100111,%11001110,%01001110,%01110001,%11111001,%10011111,%11111111
	.db %11111111,%10011100,%10011100,%10011111,%11110010,%01100111,%11001110,%01001110,%01100100,%11111111,%10011111,%11111111
	.db %11111111,%10011100,%10011100,%10011111,%11110010,%01100111,%11001110,%01001110,%01100100,%11111111,%10011111,%11111111
	.db %11111111,%10011100,%10000001,%10000001,%11110010,%01100111,%11001110,%01000000,%01100100,%11111111,%00111111,%11111111
	.db %11111111,%10000001,%10011001,%10011111,%11110010,%01100111,%11000000,%11001110,%01100100,%11111110,%01111111,%11111111
	.db %11111111,%10011111,%10011100,%10011111,%11100000,%00100111,%11001111,%11001110,%01000000,%01111100,%11111111,%11111111
	.db %11111111,%10011111,%10011100,%10011111,%11100111,%00100111,%11001111,%11001110,%01001110,%01111001,%11111111,%11111111
	.db %11111111,%10011111,%10011110,%00000001,%11100111,%00100000,%01001111,%11001110,%01001110,%01111000,%00011111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
	.db %11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111,%11111111
